// pages/book-detail/book-detail.js

const requestToGetBill = require('../../utils/request.js').requestToGetBill
const toBillPayPage = require('../../utils/wx-navigate.js').toBillPayPage
const toRecordDeatilPage = require('../../utils/wx-navigate.js').toRecordDeatilPage
const toBillPage = require('../../utils/wx-navigate.js').toBillPage


//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabArray: [{ prop: '', label: '待交账单' },{ prop: '', label: '租房信息' }, { prop: '', label: '交租记录' }],
    book:{},
    notpayBillList:[],
    billList:[],
    currentIndex:0,

    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let data = options.data?JSON.parse(options.data):{}
    this.setData({
      book:data
    })

    
    this.toGetData()
    app.updateBookDetailPageData = this.toGetData

    
 
  },

  toGetData(){
    requestToGetBill(this.data.book._id, "NOTPAY", res => {
      this.setData({
        notpayBillList: res.list
      })
    })

    requestToGetBill(this.data.book._id, "SUCCESS&&CLOSED", res => {
      let billList = res.list
      for (let i = 0; i < billList.length;i++){
        let _bill = billList[i]
        if (_bill.time_end){
          _bill.time_end = _bill.time_end.substr(0, 4) + "/" + _bill.time_end.substr(4, 2) + "/" + _bill.time_end.substr(6, 2) + " " + _bill.time_end.substr(8, 2) + ":" + _bill.time_end.substr(10, 2) + ":" + _bill.time_end.substr(12, 2)
        }
        if (_bill.close_info) {
          _bill.close_info.moneyday = _bill.close_info.moneyday.substr(0, 4) + "/" + _bill.close_info.moneyday.substr(5, 2) + "/" + _bill.close_info.moneyday.substr(8, 2)
        }
        billList[i] = _bill
      }
      

      this.setData({
        billList: res.list
      })
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.toGetData()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  payTabClick(e){
    this.setData({
      currentIndex: e.detail.currentIndex
    })

  },
  bindbillitemtap(e) {
    let bill = e.currentTarget.dataset.bill
    if (bill.trade_state === "SUCCESS" || bill.trade_state === "CLOSED") {
      toRecordDeatilPage(e.currentTarget.dataset.bill)
    } else {
      toBillPayPage(e.currentTarget.dataset.bill)
    }
  },

  bindeditbooktap(){
    toBillPage()
  }
})